import Vue from 'vue';
import Vuex from 'vuex';
import {
	mapState
} from 'vuex'
Vue.use(Vuex);

let Cache = uni.getStorageSync('Cache') || {};
//永久化保存
const permanent_list = ['get_user','get_img_url'];
const saveToStorage = (key, value) => {
	if (permanent_list.includes(key)) {
		let tmpCache = uni.getStorageSync('Cache') || {};
		tmpCache[key] = value;
		uni.setStorageSync('Cache', tmpCache);
	}
};

// 初始化用户数据  
const users = {
	nickname: '请登录!',
	uid: "",
	face: "",
	banmi_token: ""
};

const store = new Vuex.Store({
	state: {
		// 从本地存储中恢复用户数据，如果没有则使用默认用户数据  
		get_user: Cache.get_user || users,
		get_status_bar_height: 0, //状态栏高度 
		get_system_set: 0, //系统配置信息
		get_plugin_set: 0, //插件配置信息
		get_img_url: "", //服务器配置的图片域名地址
		get_login: Cache.get_user ? Cache.get_user.banmi_token :
		"", //有值就代表已登录 vue文件 可以使用 this.get_login 来获取 其他同理

	},
	mutations: {
		loginOut(state, data) {
			state["get_user"] = users;
			saveToStorage("get_user", state["get_user"]);
		},
		setState(state, data) {
			const keys = data.name.split('.');
			let target = state;
			for (let i = 0; i < keys.length - 1; i++) {
				if (!(keys[i] in target)) {
					target[keys[i]] = {}; // 如果中间路径不存在，则创建空对象  
				}
				target = target[keys[i]];
			}
			// 设置最终值  
			target[keys[keys.length - 1]] = data.value;
			// 如果该key在需要保存的列表中，则保存到本地存储  
			if (permanent_list.includes(keys[0])) {
				saveToStorage(keys[0], state[keys[0]]);
			}
		}
	},
	actions: {},
	modules: {}
});
let KeyList = []
try {
	KeyList = store.state ? Object.keys(store.state) : []
} catch (e) {

}

var obj = {
	computed: {
		...mapState(KeyList)
	},

};



Vue.mixin(obj)


export default store;